<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
<!--      <div v-if="crud.props.searchToggle">
        &lt;!&ndash; 搜索 &ndash;&gt;
        <el-select
          v-model="query.lineCode"
          clearable
          filterable
          placeholder="产线编号"
          style="width: 220px;"
          class="filter-item"
          @change="lineCodeChange"
        >
          <el-option
            v-for="item in lines"
            :key="item.id"
            :label="item.modelName"
            :value="item.modelCode"
          >
            <span style="float: left">{{ item.modelName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.modelCode }}</span>
          </el-option>
        </el-select>

        <el-select
          v-model="query.sectionCode"
          clearable
          filterable
          placeholder="工段编号"
          style="width: 220px;"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in sections"
            :key="item.id"
            :label="item.modelName"
            :value="item.modelCode"
          >
            <span style="float: left">{{ item.modelName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.modelCode }}</span>
          </el-option>
        </el-select>
        <el-select v-model="query.isDisable" clearable filterable
                   placeholder="是否禁用" style="width: 185px;" class="filter-item" @change="crud.toQuery" >
          <el-option
                  v-for="item in dict.is_disable"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      &lt;!&ndash;如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'&ndash;&gt;
      <crudOperation :permission="permission" />-->
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="产线编号" prop="lineCode">
            <el-select @change="lineCodeChange" v-model="form.lineCode" style="width: 370px" filterable placeholder="请选择">
              <el-option
                v-for="item in lines"
                :key="item.id"
                :label="item.modelName"
                :value="item.modelCode" >
                <span style="float: left">{{ item.modelName }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.modelCode }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="工段编号" prop="sectionCode">
            <el-select v-model="form.sectionCode" style="width: 370px" filterable placeholder="请选择">
              <el-option
                v-for="item in sections"
                :key="item.id"
                :label="item.modelName"
                :value="item.modelCode" >
                <span style="float: left">{{ item.modelName }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.modelCode }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否禁用" prop="isDisable">
            <el-radio-group v-model="form.isDisable">
              <el-radio :label="item.value" v-for="item in dict.is_disable">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>

      <el-row :gutter="20">
        <el-col :span="10">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>混线配置</span>
            </div>
            <!--工具栏-->
            <div class="head-container">
              <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
              <crudOperation :permission="permission" />
            </div>
            <!--表格渲染-->
            <el-table ref="table"
                      v-loading="crud.loading"
                      :data="crud.data"
                      size="small"
                      style="width: 100%;"
                      @selection-change="crud.selectionChangeHandler"
                      @current-change="handleCurrentChange"
            >
              <el-table-column type="selection" width="55" />
              <el-table-column prop="lineCode" label="产线编码" />
              <el-table-column prop="sectionCode" label="工段编码" />
              <el-table-column prop="isDisable" label="是否禁用">
                <template slot-scope="scope">
                  {{ dict.label.is_disable[scope.row.isDisable] }}
                </template>
              </el-table-column>
              <el-table-column prop="remark" label="描述" />
              <el-table-column prop="createBy" label="创建人" />
              <el-table-column prop="createTime" label="创建时间" />
              <el-table-column prop="updateBy" label="更新人" />
              <el-table-column prop="updateTime" label="更新时间" />
              <el-table-column v-if="checkPer(['admin','planMixedLine:edit','planMixedLine:del'])" fixed="right" label="操作" width="150px" align="center">
                <template slot-scope="scope">
                  <udOperation
                    :data="scope.row"
                    :permission="permission"
                  />
                </template>
              </el-table-column>
            </el-table>
            <!--分页组件-->
            <pagination />
          </el-card>
        </el-col>
        <el-col :span="14">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>规则列表</span>
            </div>
            <planMixedRule ref="planMixedRule"></planMixedRule>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import crudPlanMixedLine from '@/api/plan/planMixedLine'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import planMixedRule from "../mixed_rule/index"
import crudFactoryModel from "@/api/factory/factoryModel";

const defaultForm = { id: null, lineCode: null, sectionCode: null, isDisable: '0', remark: null, createBy: null, createTime: null, updateBy: null, updateTime: null }
export default {
  name: 'PlanMixedLine',
  components: { pagination, crudOperation, rrOperation, udOperation, planMixedRule },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['is_disable'],
  cruds() {
    return CRUD({
      title: '混线规则',
      url: 'api/planMixedLine',
      idField: 'id',
      sort: 'id,desc',
      optShow: {
      add: true,
        edit: false,
        del: false,
        download: false,
        reset: false,
        import: false
      },
      crudMethod: { ...crudPlanMixedLine }})
  },
  data() {
    return {
      lines: [],
      sections: [],
      permission: {
        add: ['admin', 'planMixedLine:add'],
        edit: ['admin', 'planMixedLine:edit'],
        del: ['admin', 'planMixedLine:del']
      },
      rules: {
        lineCode: [
          { required: true, message: '产线编码不能为空', trigger: 'blur' }
        ],
        sectionCode: [
          { required: true, message: '工段编码不能为空', trigger: 'blur' }
        ],
        isDisable:[
          { required: true, message: '是否禁用不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'ID' },
        { key: 'lineCode', display_name: '产线编码' },
        { key: 'sectionCode', display_name: '工段编码' },
        { key: 'isDisable', display_name: '是否禁用' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    //新增之前
    [CRUD.HOOK.beforeToAdd]() {
      this.sections = [];
      return true
    },
    //编辑之前
    [CRUD.HOOK.beforeToEdit](crud) {
      this.lineCodeChange(crud.form.lineCode)
      return true
    },
    //产线选择
    lineCodeChange(lineCode){
      let id = 0;
      this.lines.forEach(res => {
        if(res.modelCode == lineCode){
          id = res.id;
          return;
        }
      })
      crudFactoryModel.getModels({pid: id ,modelType: '3'}).then(res =>{
        this.sections = res.content;
      });
    },
    // 选中类型后，展示产品详情
    handleCurrentChange(val) {
      if (val) {
        this.$refs.planMixedRule.queryPage(val.id,val.lineCode,val.sectionCode);
      }
    }
  },
  mounted() {
    crudFactoryModel.getModels({modelType: '2'}).then(res =>{
      this.lines = res.content;
    });
  }

}
</script>

<style scoped>

</style>
